package com.itheima.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.goods.mapper.BrandDao;
import com.itheima.goods.pojo.Brand;
import com.itheima.goods.service.BrandService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandDao, Brand> implements BrandService {

    /**
     * 查询所有的品牌信息
     * @return
     */
    @Override
    public Result findAllBrand() {
        QueryWrapper<Brand> wrapper = new QueryWrapper<>();
        wrapper.ge("id",1);
        List<Brand> brands = baseMapper.selectList(wrapper);
        return new Result(true, StatusCode.OK,"查询成功",brands);
    }

    /**
     * 添加品牌信息
     * @param brand
     * @return
     */
    @Override
    public Result addBrand(Brand brand) {
        int count = baseMapper.insert(brand);
        return new Result(true,StatusCode.OK,"添加成功",count);
    }

    /**
     * 品牌的分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Result findBrandByPage(Integer page, Integer size) {

        IPage<Brand> ipage = new Page<>(page,size);
        page(ipage);
        PageResult brandPageResult = new PageResult<>(ipage.getTotal(), ipage.getRecords());

        return new Result(true,StatusCode.OK,"查询成功",brandPageResult);
    }

    /**
     * 根据id查询品牌信息
     * @param id
     * @return
     */
    @Override
    public Result findById(Integer id) {
        QueryWrapper<Brand> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        List<Brand> brands = baseMapper.selectList(wrapper);
        return new Result(true,StatusCode.OK,"查询成功",brands);
    }

    @Override
    public Result updateBrandById(Integer id, Brand brand) {
        QueryWrapper<Brand> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        int count = baseMapper.update(brand, wrapper);
        return new Result(true,StatusCode.OK,"修改成功",count);
    }

    @Override
    public Result deleteBrandById(Integer id) {
        QueryWrapper<Brand> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        int count = baseMapper.delete(wrapper);
        return new Result(true,StatusCode.OK,"删除成功",count);
    }
}
